package com.zzhrtech.wgzx_cityzen.ui.contact;

import android.app.Activity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.GsonRequest;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.zzhrtech.WgzxApplication;
import com.zzhrtech.wgzx_cityzen.BaseActivity;
import com.zzhrtech.wgzx_cityzen.Constans;
import com.zzhrtech.wgzx_cityzen.MainActivity;
import com.zzhrtech.wgzx_cityzen.R;
import com.zzhrtech.wgzx_cityzen.adapters.contact.GoodsRecyclerViewAdapter;
import com.zzhrtech.wgzx_cityzen.base.OnItemClickLitener;
import com.zzhrtech.wgzx_cityzen.beans.contact.GoodClassBean;
import com.zzhrtech.wgzx_cityzen.beans.contact.JifenGoodsBean;
import com.zzhrtech.wgzx_cityzen.beans.me.LoginBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsActivity extends BaseActivity {

    private RecyclerView rc_goods;

    private GoodsRecyclerViewAdapter dhAdapter;



    private RadioGroup group_class;
    private List<GoodClassBean.Data> datas;

    public boolean islist = true;

    private List<JifenGoodsBean.Data> dataList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);

        toolbar.setNavigationIcon(R.mipmap.ic_close);

        group_class = (RadioGroup) findViewById(R.id.radioGroup);

        group_class.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                httpGetRole(datas.get(checkedId).getCat_id());

            }
        });


        rc_goods = (RecyclerView) findViewById(R.id.swipe_target);

        rc_goods.setLayoutManager(new LinearLayoutManager(this));
//        rc_goods.setLayoutManager(new GridLayoutManager(GoodsActivity.this, 2));

        rc_goods.setItemAnimator(new DefaultItemAnimator());

        dhAdapter = new GoodsRecyclerViewAdapter(this);

        rc_goods.setAdapter(dhAdapter);

        dhAdapter.setOnItemClickLitener(new OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                if (dhAdapter.getGoodsInen(position) == 0) {
                    showToast("暂时没有该商品!");
                } else {
                    Intent intent = new Intent(GoodsActivity.this, JifenDetailActivity.class);
                    intent.putExtra("goodid", dhAdapter.getGoodsId(position));
                    intent.putExtra("acid", getIntent().getStringExtra("activid"));
                    startActivityForResult(intent, 2);
                }
            }
        });

        httpGetActivClass();
    }

    //获取商品列表
    private void httpGetRole(String catId) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("cat_id", catId);
        GsonRequest<JifenGoodsBean> gsonRequest = new GsonRequest<JifenGoodsBean>(Request.Method.POST, Constans.HTTP_GOODSLISTBYCAT, JifenGoodsBean.class, params, new com.android.volley.Response.Listener<JifenGoodsBean>() {
            @Override
            public void onResponse(JifenGoodsBean response) {

                if (response.isSuccess()) {
                    Message message = new Message();
                    message.what = 0;
                    message.obj = response.getData();
                    mHandler.sendMessage(message);
                } else {
//                    emptyView.setVisibility(View.VISIBLE);
                    dhAdapter.cleanList();
                    dhAdapter.notifyDataSetChanged();
//                    showToast(response.getMsg());
                }

            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("result=====", error + "");
            }
        });

        WgzxApplication.addRequest(gsonRequest, Constans.reqTag);
    }




    private void httpGetActivClass() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("activity_id", getIntent().getStringExtra("activid"));
        GsonRequest<GoodClassBean> gsonRequest = new GsonRequest<GoodClassBean>(com.android.volley.Request.Method.POST, Constans.HTTP_GOODSLISTCLASS, GoodClassBean.class, params, new com.android.volley.Response.Listener<GoodClassBean>() {
            @Override
            public void onResponse(GoodClassBean response) {
                if (response.isSuccess()) {
                    Message message = new Message();
                    message.what = 4;
                    message.obj = response.getData();
                    mHandler.sendMessage(message);
                } else {
                    showToast(response.getMsg());
                }
            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("result=====", error + "");
            }
        });

        WgzxApplication.addRequest(gsonRequest, Constans.reqTag);
    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            if (msg.what == 0) {
                dataList = (List<JifenGoodsBean.Data>) msg.obj;
                dhAdapter.cleanList();
                for (JifenGoodsBean.Data data : dataList) {
                    dhAdapter.addItem(data);
                }
            } else if (msg.what == 4) {
                datas = (List<GoodClassBean.Data>) msg.obj;

                for (int i = 0; i < datas.size(); i++) {
                    RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    params.leftMargin = 12;
                    RadioButton button = new RadioButton(GoodsActivity.this);
                    button.setText(datas.get(i).getCat_name());
                    button.setId(i);
                    button.setButtonDrawable(0);
                    button.setPadding(28, 0, 28, 0);
                    ColorStateList colors = getResources().getColorStateList(R.color.radio_color);
                    button.setTextColor(colors);
                    button.setTextSize(12);
                    button.setBackgroundResource(R.drawable.redio_background);
                    button.setLayoutParams(params);
                    group_class.addView(button);

                    if (i == 0) {
                        group_class.check(button.getId());
                    }

                }
            }

            return false;
        }
    });

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 2 & resultCode == RESULT_OK){

            setResult(RESULT_OK);
            finish();
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_goods, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.change:
                if (islist) {
                    rc_goods.setLayoutManager(new GridLayoutManager(GoodsActivity.this, 2));
                    islist = false;
                } else {
                    rc_goods.setLayoutManager(new LinearLayoutManager(GoodsActivity.this));
                    islist = true;
                }

                break;

        }

        return super.onOptionsItemSelected(item);
    }
}
